import json

from flask import Flask, Response, request, Blueprint
from flask_jwt_extended import JWTManager, create_access_token, jwt_required, get_jwt_identity

from app.service.SearchService import SearchService
from app.common.commonMessage import CommonMassages
from app.config.db import DBUtils
from app.config.jsonEncoder import CustomJSONEncoder

app = Flask(__name__)
# 创建蓝图
search_bp = Blueprint('search_control', __name__,url_prefix='/kshx')

# 服务
searchService = SearchService()

# 获取session
db_utils = DBUtils()
session = db_utils.get_session()

@search_bp.route('/search', methods=['GET'])
@jwt_required()
def findAll():
    searchInput = request.args.get("searchInput")
    resultcount = request.args.get("resultcount")
    pagenum = request.args.get("pagenum")
    dwmc = request.args.get("searchCompanyType")
    print(dwmc)
    if dwmc == '神舟软件':
        gs = '航天软件'
    elif dwmc == '集团总部':
        gs = '集团公司总部'
    elif dwmc == '资源卫星':
        gs = '资源卫星中心'
    else:
        gs = dwmc

    if searchInput and resultcount and pagenum:
        data = searchService.select_companyName(session, searchInput, resultcount, pagenum, gs)
        if data is not None:
            result = {
                "code": 200,
                "data": data,
                "message": CommonMassages.CCD_SELECT_SUCCESS.value
            }
        else:
            result = {
                "code": 400,
                "data": None,
                "message": CommonMassages.CCD_SELECT_FAILED.value
            }
    else:
        result = {
            "code": 400,
            "data": None,
            "message": CommonMassages.CCD_SELECT_FAILED.value
        }
    resp_data = json.dumps(result, ensure_ascii=False, cls=CustomJSONEncoder)
    # print(resp_data)
    # return Response(resp_data, content_type='application/json')
    return Response(resp_data)


